package com.shuangti.food.controller;


import com.shuangti.food.bean.FoodNutritionTbl;
import com.shuangti.food.service.FoodNutritionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/food")
@Validated
public class FoodNutritionController {
    @Autowired
    FoodNutritionService foodNutritionService;

    @PostMapping("/add")
    public Map<String,String> addFood(FoodNutritionTbl foodNutritionTbl){
        foodNutritionService.addFood(foodNutritionTbl);

        Map<String,String> result = new HashMap<>();
        result.put("200","添加成功");

        return result;
    }

    @PostMapping("/del")
    public Map<String,String> delFood(Long food_id){
        foodNutritionService.delFood(food_id);

        Map<String,String> result = new HashMap<>();
        result.put("200","删除成功");

        return result;
    }

    @PostMapping("/updateFoodInfo")
    public Map<String,String> updateUserInfo(FoodNutritionTbl foodNutritionTbl){
        Map<String, String> result = new HashMap<>();
        if(foodNutritionService.updateFoodInfo(foodNutritionTbl)){
            result.put("200","修改成功");
        }
        else {
            result.put("400","修改失败");
        }
        return  result;
    }

    @PostMapping("/findFood")
    public List<FoodNutritionTbl> findFoodByName(String name){
        return foodNutritionService.findByFoodName(name);
    }

    @PostMapping("/test")
    public String findFoodById(@RequestBody Long food_id){
        return foodNutritionService.testRedis(food_id);
    }
}
